package com.kung.qvs.ui.main.fragment;

import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.afollestad.materialdialogs.MaterialDialog;
import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.app.AppViewModelFactory;
import com.kung.qvs.databinding.FragSettingBinding;
import com.kung.qvs.ui.main.KungViewModel;
import com.kung.qvs.ui.widget.ConformDialog;
import com.kung.qvs.utils.RestartAPPTool;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 收集的垃圾记录
 */
public class SettingFrag extends BaseFragment<FragSettingBinding,SettingViewModel> {

    private KungViewModel kungViewModel;
    private MaterialDialog mSimpleDialog;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_setting;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SettingViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(getActivity(), factory).get(SettingViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
    }

    @Override
    public void initData() {
        super.initData();
        kungViewModel = new ViewModelProvider(getActivity()).get(KungViewModel.class);
        kungViewModel.setmIsHomeFrag(false);

        PackageManager manager = getActivity().getPackageManager();
        PackageInfo info;
        try {
            info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            viewModel.versionName.set(info.versionName);
            viewModel.versionCode.set(info.versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.eventToLoginPage.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                //关闭页面，返回首页
                Navigation.findNavController(getView()).navigateUp();
            }
        });

        viewModel.uc.eventToSetBaseUrl.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                mSimpleDialog = new MaterialDialog.Builder(getActivity()).title("设置服务器地址")
                        .content("设置服务器地址")
                        .inputType(InputType.TYPE_CLASS_TEXT)
                        .input("服务器地址", viewModel.getBaseUrl(), new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                viewModel.setBaseUrl(input.toString());
                                ToastUtils.showShort("IP地址设置成功!");
                                //延时1秒重启app,让sp能保存值
                                new Handler().postAtTime(new Runnable() {
                                    @Override
                                    public void run() {
                                        //("如果你用的是Retrofit2,不重启ip地址切换不会生效")
                                        //一秒后重启app
                                        RestartAPPTool.restartAPP(getActivity());
                                    }
                                }, 1000);
                            }
                        })
                        .positiveText("确定")
                        .show();
            }
        });

        viewModel.uc.eventToCompanyInfo.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Bundle mBundle = new Bundle();
                Navigation.findNavController(getView()).navigate(R.id.fragWorkers, mBundle);
            }
        });

        viewModel.uc.eventToExitApp.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                ConformDialog.Builder builder = new ConformDialog.Builder(getActivity());
                builder.setMessage("是否系统退出?", "退出将无法使用应用");
                builder.setTitle("退出");
                builder.setPositiveButton("退出系统",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                                viewModel.doFunExit();
                            }
                        });

                builder.setNegativeButton("继续使用",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        viewModel.uc.eventToResetPwd.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Bundle mBundle = new Bundle();
                Navigation.findNavController(getView()).navigate(R.id.pwdResetFrag, mBundle);
            }
        });

        viewModel.uc.eventToSupplierPage.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Bundle mBundle = new Bundle();
                Navigation.findNavController(getView()).navigate(R.id.supplierManageFrag, mBundle);
            }
        });

        viewModel.uc.eventToCustomerPage.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Bundle mBundle = new Bundle();
                Navigation.findNavController(getView()).navigate(R.id.customerManageFrag, mBundle);
            }
        });

        viewModel.uc.eventToManageProduct.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Bundle mBundle = new Bundle();
                Navigation.findNavController(getView()).navigate(R.id.fragProducts, mBundle);
            }
        });
    }
}
